package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_coupon_detail;

import cn.com.api.service.ApiU_coupon_detailService;

import cn.com.util.*;


public class U_coupon_detailBaseController {

	private static Logger logger = Logger.getLogger(U_coupon_detailBaseController.class);
	
	@Autowired
	private ApiU_coupon_detailService apiU_coupon_detailService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_coupon_detail-list")
	public ModelAndView u_coupon_detail_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_coupon_detail_list.jsp");
		
		List<ApiU_coupon_detail> u_coupon_details = apiU_coupon_detailService.findU_coupon_details();
		mv.addObject("u_coupon_details", u_coupon_details);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_coupon_detail-add-pre")
	public ModelAndView u_coupon_detail_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_coupon_detail_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiU_coupon_detail u_coupon_detail = apiU_coupon_detailService.findU_coupon_detailById(Integer.valueOf(id));
			mv.addObject("u_coupon_detail",u_coupon_detail);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_coupon_detail-add")
	public void u_coupon_detail_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String coupon_detail_id = request.getParameter("coupon_detail_id");
		String coupon_id = request.getParameter("coupon_id");
		String order_no = request.getParameter("order_no");
		String user_id = request.getParameter("user_id");
		String use_time = request.getParameter("use_time");
		String status = request.getParameter("status");
		String is_balance = request.getParameter("is_balance");
		String balance_price = request.getParameter("balance_price");
		String balance_time = request.getParameter("balance_time");
		String coupon_code = request.getParameter("coupon_code");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");
		String ext4 = request.getParameter("ext4");
		String ext5 = request.getParameter("ext5");
		
		Map<String,Object> map = new HashMap<String,Object>();
		//String id = request.getParameter("id");
		//add
		if(coupon_detail_id==null || (coupon_detail_id.trim()).equals("")){
			ApiU_coupon_detail u_coupon_detail = new ApiU_coupon_detail();
            /*
		    u_coupon_detail.setCoupon_detail_id(coupon_detail_id);
		    u_coupon_detail.setCoupon_id(coupon_id);
		    u_coupon_detail.setOrder_no(order_no);
		    u_coupon_detail.setUser_id(user_id);
		    u_coupon_detail.setUse_time(use_time);
		    u_coupon_detail.setStatus(status);
		    u_coupon_detail.setIs_balance(is_balance);
		    u_coupon_detail.setBalance_price(balance_price);
		    u_coupon_detail.setBalance_time(balance_time);
		    u_coupon_detail.setCoupon_code(coupon_code);
		    u_coupon_detail.setExt1(ext1);
		    u_coupon_detail.setExt2(ext2);
		    u_coupon_detail.setExt3(ext3);
		    u_coupon_detail.setExt4(ext4);
		    u_coupon_detail.setExt5(ext5);
	        */
			apiU_coupon_detailService.insertU_coupon_detail(u_coupon_detail);
		}
		//edit
		else{
			ApiU_coupon_detail u_coupon_detail = apiU_coupon_detailService.findU_coupon_detailById(Integer.valueOf(coupon_detail_id));
	        /*
		    u_coupon_detail.setCoupon_detail_id(coupon_detail_id);
		    u_coupon_detail.setCoupon_id(coupon_id);
		    u_coupon_detail.setOrder_no(order_no);
		    u_coupon_detail.setUser_id(user_id);
		    u_coupon_detail.setUse_time(use_time);
		    u_coupon_detail.setStatus(status);
		    u_coupon_detail.setIs_balance(is_balance);
		    u_coupon_detail.setBalance_price(balance_price);
		    u_coupon_detail.setBalance_time(balance_time);
		    u_coupon_detail.setCoupon_code(coupon_code);
		    u_coupon_detail.setExt1(ext1);
		    u_coupon_detail.setExt2(ext2);
		    u_coupon_detail.setExt3(ext3);
		    u_coupon_detail.setExt4(ext4);
		    u_coupon_detail.setExt5(ext5);
			*/
			apiU_coupon_detailService.updateU_coupon_detail(u_coupon_detail);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_coupon_detail-delete")
	public void u_coupon_detail_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiU_coupon_detailService.deleteU_coupon_detail(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



